# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :get_robot.py
# @Time      :2024/12/9 10:10:26
# @Author    :flower

from dingtalkchatbot.chatbot import DingtalkChatbot

from datetime import datetime

from util.get_allure import GetAllure
from util.get_http import GetHttp


class GetRobot:

    @staticmethod
    def dingtalk_robot(webhook_1, secret_1):
        allure_result = GetAllure.get_allure()
        robot_dog = DingtalkChatbot(webhook=webhook_1, secret=secret_1)

        now_time = datetime.now().strftime('%Y.%m.%d %H:%M:%S')
        robot_dog.send_markdown(
            title=f'来自自动化机器人的提醒',
            text=f'### **接口自动化测试**\n'
                 f'**发送 时间:**  {now_time}\n\n'
                 f'**执行通过率:**  {allure_result.case_rate}%\n\n'
                 f'**执行用例数:**  {allure_result.case_all}个\n\n'
                 f'<font color="#006400">**成功用例数:**</font>  <font color="#006400">{allure_result.case_pass}个</font>\n\n'
                 f'<font color="#dd0000">**失败用例数:**</font>  <font color="#dd0000">{allure_result.case_fail}个</font>\n\n'
                 f'**测试 报告:**  http://192.168.123.54:9090/index.html\n\n',
            is_at_all=True)

    # @staticmethod
    # def send_ding():
    #     case_all, case_fail, case_pass, case_rate = GetAllure.get_allure()
    #     text = f"自动化用例通过率：{case_rate}%" \
    #            f"\n执行用例数：{case_all}个" \
    #            f"\n成功用例数：{case_pass}个" \
    #            f"\n失败用例数：{case_fail}个",
    #     data = {
    #         "msgtype": "text",
    #         "text":{
    #             "content": "%s" % text,
    #         },
    #         "isAtAll": True
    #     }
    #     url = 'https://oapi.dingtalk.com/robot/send?access_token=9295d0b2177891558c64c519e801adad9ca34461287d89f7ae6bfbbed6b44ded'
    #     response = GetHttp().do_post_request(url=url, data=data)
    #     return response


if __name__ == "__main__":
    webhook = 'https://oapi.dingtalk.com/robot/send?access_token=9295d0b2177891558c64c519e801adad9ca34461287d89f7ae6bfbbed6b44ded'
    secrets = 'SEC238b8f2a899e15339701f60ef95f42421f9867faa1a10082903ec2b2c6944cbe'
    GetRobot().dingtalk_robot(webhook_1=webhook, secret_1=secrets)
    # res = GetRobot().send_ding()
    # print(res)
